package com.qf.smsplatform.strategy.service.impl;

import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.strategy.service.Strategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: LeeRui
 * 2021/1/27 下午 9:18
 * 路由策略(所有策略通过后，通过路由策略将封装好的StandardSubmit发送到MQ队列，供网关模块消费)
 **/
@Service(value = "router")
@Slf4j
public class RouterStrategy implements Strategy {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("【路由策略】 路由策略开始执行！！！");
        //准备队列名称
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY+standardSubmit.getGatewayID();
        //创建队列
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());

        //向MQ指定队列发送消息，供网关模块消费
        rabbitTemplate.convertAndSend(queueName,standardSubmit);
        //记录日志
        log.info("【路由策略】 路由策略执行成功，消息成功发送到MQ队列，等待网关模块消费！！！");
        return true;
    }
}
